<?php

class Store extends CI_Model {
    function __construct()
    {
        parent::__construct();
    }
    
   public function getStoreFromDB() {
   	
   		$this->config->load('config_maitai_tables');
   	
		// Check if DB TBL 
   		$this->db->cache_on();
   		$tbl_stores = $this->config->item('table_stores');
		$query = $this->db->get($tbl_stores);
		
   		if($q = $query->result_array())
		{
			// Has existing content
			//print_r($q); die();
			return $q;
			
		} else {
			// Has no item inside TBL

			// connect to Hessian to fetch new items
			$storesFromAPI = $this->getStoresFromAPI();
			//var_dump($gadgetsFromAPI);
			//die();
			// truncate TOUCH MOOOBB
			$this->db->truncate($tbl_stores);
			
			// store to DB
			$dataArr = array();
			foreach($storesFromAPI as $stores) {
				$data['StoreID'] = $stores->storeID;
				$data['StoreName'] = $stores->storeName;
				$data['StoreAddress'] = $stores->storeAddress;
				$data['StoreHours'] = $stores->storeHours;
				$data['StoreArea'] = $stores->storeArea;
				$data['Longitude'] = $stores->longitude;
				$data['Latitude'] = $stores->latitude;				
				array_push($dataArr, $data);
			}
			
			$this->db->insert_batch($tbl_stores, $dataArr); 

			//print_r($dataArr);
			$q = $dataArr;
		}
		
		$this->db->cache_off();
		$this->db->close();
		return $q;
   }

	public function truncateStore() {
		$tbl_stores = $this->config->item('table_stores');
		$this->db->truncate($tbl_stores);
	}
   public function getStore($lat,$lng,$rad=false, $region=false){
  	   /*  . . . this function is needed for getting the nearest store whether by region or proximity . . . */
  	   
	   if($region == false){	
	  	/*  locator is nearest  */
	  	$querystring = "SELECT StoreId,StoreName, StoreHours,StoreAddress,StoreArea,Latitude,Longitude, ( 6371 * acos( cos( radians(".$lat.") ) * cos( radians(Latitude ) ) * cos( radians( Longitude ) - radians(".$lng.") ) + sin(radians(".$lat.") ) * sin( radians( Latitude ) ) ) ) AS distance FROM Maitai_Stores HAVING distance < ".$rad."  ORDER BY distance ;";
		$query = $this->db->query($querystring);
	 	if($query->num_rows == 0){
		   return false; 
		}  
	    // to do add control for adding more radius option			 
		//echo"get store from model <br/>$querystring<br/>";var_dump($query->result_array() ); 
		return $query->result_array();	 	 
	  }
	  else if($rad == false){
		/*  locator is preferred   */
		$query = $this->db->query("SELECT StoreId,StoreName, StoreHours,StoreAddress,StoreArea,Latitude,Longitude FROM Maitai_Stores where StoreArea = '".$region."' ;");
	 	if($query->num_rows == 0){
		   return false; 
		}  
		 
		return $query->result_array();	 	
	 }
	}
   
    public function getStoresFromAPI($name="", $address="", $area="")
	{
   		$this->load->library('Maitaidata', '', 'Mtdata');
   		$stores = $this->Mtdata->getStoreList($name, $address, $area);
   		$status = $stores->status;
   		if($status==0){
   			return $stores->obj;
   		} else {
   			return false;
   		}
   }
   
}